{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import sys\n",
    "import os\n",
    "# 添加项目目录到 Python 路径\n",
    "project_path = r\"D:\\code\\machine-learning\\My_LightBoost\"\n",
    "if project_path not in sys.path:\n",
    "    sys.path.append(project_path)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "closing parenthesis ']' does not match opening parenthesis '(' (_lightgbm.py, line 41)",
     "output_type": "error",
     "traceback": [
      "Traceback \u001B[1;36m(most recent call last)\u001B[0m:\n",
      "\u001B[0m  File \u001B[0;32m~\\AppData\\Local\\Programs\\Python\\Python312\\Lib\\site-packages\\IPython\\core\\interactiveshell.py:3577\u001B[0m in \u001B[0;35mrun_code\u001B[0m\n    exec(code_obj, self.user_global_ns, self.user_ns)\u001B[0m\n",
      "\u001B[1;36m  Cell \u001B[1;32mIn[7], line 1\u001B[1;36m\n\u001B[1;33m    import _lightgbm  # 首次导入\u001B[1;36m\n",
      "\u001B[1;36m  File \u001B[1;32mD:\\code\\machine-learning\\My_LightBoost\\_lightgbm.py:41\u001B[1;36m\u001B[0m\n\u001B[1;33m    return self.base_pred + self.learning_rate * sum(self.predict_tree(X, tree) for tree in self.trees])\u001B[0m\n\u001B[1;37m                                                                                                      ^\u001B[0m\n\u001B[1;31mSyntaxError\u001B[0m\u001B[1;31m:\u001B[0m closing parenthesis ']' does not match opening parenthesis '('\n"
     ]
    }
   ],
   "source": [
    "import _lightgbm  # 首次导入\n",
    "from importlib import reload\n",
    "reload(_lightgbm)\n",
    "import numpy as np\n",
    "import pandas as pd\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "outputs": [],
   "source": [
    "import pandas as pd"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\BLACK\\AppData\\Local\\Temp\\ipykernel_23796\\1257963872.py:4: FutureWarning: DataFrame.fillna with 'method' is deprecated and will raise in a future version. Use obj.ffill() or obj.bfill() instead.\n",
      "  iris_data = iris_data.fillna(method=\"ffill\")\n"
     ]
    }
   ],
   "source": [
    "iris_data = pd.read_csv(\"C:\\\\Users\\\\BLACK\\\\Downloads\\\\iris.csv\")\n",
    "from sklearn.preprocessing import LabelEncoder\n",
    "iris_data[\"Species\"] = LabelEncoder().fit_transform(iris_data[\"Species\"])\n",
    "iris_data = iris_data.fillna(method=\"ffill\")\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "outputs": [
    {
     "ename": "ValueError",
     "evalue": "The least populated class in y has only 1 member, which is too few. The minimum number of groups for any class cannot be less than 2.",
     "output_type": "error",
     "traceback": [
      "\u001B[1;31m---------------------------------------------------------------------------\u001B[0m",
      "\u001B[1;31mValueError\u001B[0m                                Traceback (most recent call last)",
      "Cell \u001B[1;32mIn[15], line 4\u001B[0m\n\u001B[0;32m      1\u001B[0m \u001B[38;5;28;01mfrom\u001B[39;00m  \u001B[38;5;21;01msklearn\u001B[39;00m\u001B[38;5;21;01m.\u001B[39;00m\u001B[38;5;21;01mmodel_selection\u001B[39;00m \u001B[38;5;28;01mimport\u001B[39;00m StratifiedShuffleSplit\n\u001B[0;32m      3\u001B[0m split \u001B[38;5;241m=\u001B[39m StratifiedShuffleSplit(n_splits\u001B[38;5;241m=\u001B[39m\u001B[38;5;241m1\u001B[39m, test_size\u001B[38;5;241m=\u001B[39m\u001B[38;5;241m0.2\u001B[39m)\n\u001B[1;32m----> 4\u001B[0m \u001B[38;5;28;43;01mfor\u001B[39;49;00m\u001B[43m \u001B[49m\u001B[43mtrain_indices\u001B[49m\u001B[43m,\u001B[49m\u001B[43mtest_indices\u001B[49m\u001B[43m \u001B[49m\u001B[38;5;129;43;01min\u001B[39;49;00m\u001B[43m \u001B[49m\u001B[43msplit\u001B[49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43msplit\u001B[49m\u001B[43m(\u001B[49m\u001B[43miris_data\u001B[49m\u001B[43m,\u001B[49m\u001B[43miris_data\u001B[49m\u001B[43m[\u001B[49m\u001B[43m[\u001B[49m\u001B[38;5;124;43m\"\u001B[39;49m\u001B[38;5;124;43mSpecies\u001B[39;49m\u001B[38;5;124;43m\"\u001B[39;49m\u001B[43m,\u001B[49m\u001B[38;5;124;43m\"\u001B[39;49m\u001B[38;5;124;43mPetal.Width\u001B[39;49m\u001B[38;5;124;43m\"\u001B[39;49m\u001B[43m,\u001B[49m\u001B[38;5;124;43m\"\u001B[39;49m\u001B[38;5;124;43mSepal.Width\u001B[39;49m\u001B[38;5;124;43m\"\u001B[39;49m\u001B[43m]\u001B[49m\u001B[43m]\u001B[49m\u001B[43m)\u001B[49m\u001B[43m:\u001B[49m\n\u001B[0;32m      5\u001B[0m \u001B[43m    \u001B[49m\u001B[43mtrain_df\u001B[49m\u001B[43m \u001B[49m\u001B[38;5;241;43m=\u001B[39;49m\u001B[43m \u001B[49m\u001B[43miris_data\u001B[49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43mloc\u001B[49m\u001B[43m[\u001B[49m\u001B[43mtrain_indices\u001B[49m\u001B[43m]\u001B[49m\n\u001B[0;32m      6\u001B[0m \u001B[43m    \u001B[49m\u001B[43mtest_df\u001B[49m\u001B[43m \u001B[49m\u001B[38;5;241;43m=\u001B[39;49m\u001B[43m \u001B[49m\u001B[43miris_data\u001B[49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43mloc\u001B[49m\u001B[43m[\u001B[49m\u001B[43mtest_indices\u001B[49m\u001B[43m]\u001B[49m\n",
      "File \u001B[1;32m~\\AppData\\Local\\Programs\\Python\\Python312\\Lib\\site-packages\\sklearn\\model_selection\\_split.py:1909\u001B[0m, in \u001B[0;36mBaseShuffleSplit.split\u001B[1;34m(self, X, y, groups)\u001B[0m\n\u001B[0;32m   1879\u001B[0m \u001B[38;5;250m\u001B[39m\u001B[38;5;124;03m\"\"\"Generate indices to split data into training and test set.\u001B[39;00m\n\u001B[0;32m   1880\u001B[0m \n\u001B[0;32m   1881\u001B[0m \u001B[38;5;124;03mParameters\u001B[39;00m\n\u001B[1;32m   (...)\u001B[0m\n\u001B[0;32m   1906\u001B[0m \u001B[38;5;124;03mto an integer.\u001B[39;00m\n\u001B[0;32m   1907\u001B[0m \u001B[38;5;124;03m\"\"\"\u001B[39;00m\n\u001B[0;32m   1908\u001B[0m X, y, groups \u001B[38;5;241m=\u001B[39m indexable(X, y, groups)\n\u001B[1;32m-> 1909\u001B[0m \u001B[43m\u001B[49m\u001B[38;5;28;43;01mfor\u001B[39;49;00m\u001B[43m \u001B[49m\u001B[43mtrain\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[43mtest\u001B[49m\u001B[43m \u001B[49m\u001B[38;5;129;43;01min\u001B[39;49;00m\u001B[43m \u001B[49m\u001B[38;5;28;43mself\u001B[39;49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43m_iter_indices\u001B[49m\u001B[43m(\u001B[49m\u001B[43mX\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[43my\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[43mgroups\u001B[49m\u001B[43m)\u001B[49m\u001B[43m:\u001B[49m\n\u001B[0;32m   1910\u001B[0m \u001B[43m    \u001B[49m\u001B[38;5;28;43;01myield\u001B[39;49;00m\u001B[43m \u001B[49m\u001B[43mtrain\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[43mtest\u001B[49m\n",
      "File \u001B[1;32m~\\AppData\\Local\\Programs\\Python\\Python312\\Lib\\site-packages\\sklearn\\model_selection\\_split.py:2318\u001B[0m, in \u001B[0;36mStratifiedShuffleSplit._iter_indices\u001B[1;34m(self, X, y, groups)\u001B[0m\n\u001B[0;32m   2316\u001B[0m class_counts \u001B[38;5;241m=\u001B[39m np\u001B[38;5;241m.\u001B[39mbincount(y_indices)\n\u001B[0;32m   2317\u001B[0m \u001B[38;5;28;01mif\u001B[39;00m np\u001B[38;5;241m.\u001B[39mmin(class_counts) \u001B[38;5;241m<\u001B[39m \u001B[38;5;241m2\u001B[39m:\n\u001B[1;32m-> 2318\u001B[0m     \u001B[38;5;28;01mraise\u001B[39;00m \u001B[38;5;167;01mValueError\u001B[39;00m(\n\u001B[0;32m   2319\u001B[0m         \u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mThe least populated class in y has only 1\u001B[39m\u001B[38;5;124m\"\u001B[39m\n\u001B[0;32m   2320\u001B[0m         \u001B[38;5;124m\"\u001B[39m\u001B[38;5;124m member, which is too few. The minimum\u001B[39m\u001B[38;5;124m\"\u001B[39m\n\u001B[0;32m   2321\u001B[0m         \u001B[38;5;124m\"\u001B[39m\u001B[38;5;124m number of groups for any class cannot\u001B[39m\u001B[38;5;124m\"\u001B[39m\n\u001B[0;32m   2322\u001B[0m         \u001B[38;5;124m\"\u001B[39m\u001B[38;5;124m be less than 2.\u001B[39m\u001B[38;5;124m\"\u001B[39m\n\u001B[0;32m   2323\u001B[0m     )\n\u001B[0;32m   2325\u001B[0m \u001B[38;5;28;01mif\u001B[39;00m n_train \u001B[38;5;241m<\u001B[39m n_classes:\n\u001B[0;32m   2326\u001B[0m     \u001B[38;5;28;01mraise\u001B[39;00m \u001B[38;5;167;01mValueError\u001B[39;00m(\n\u001B[0;32m   2327\u001B[0m         \u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mThe train_size = \u001B[39m\u001B[38;5;132;01m%d\u001B[39;00m\u001B[38;5;124m should be greater or \u001B[39m\u001B[38;5;124m\"\u001B[39m\n\u001B[0;32m   2328\u001B[0m         \u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mequal to the number of classes = \u001B[39m\u001B[38;5;132;01m%d\u001B[39;00m\u001B[38;5;124m\"\u001B[39m \u001B[38;5;241m%\u001B[39m (n_train, n_classes)\n\u001B[0;32m   2329\u001B[0m     )\n",
      "\u001B[1;31mValueError\u001B[0m: The least populated class in y has only 1 member, which is too few. The minimum number of groups for any class cannot be less than 2."
     ]
    }
   ],
   "source": [
    "from  sklearn.model_selection import StratifiedShuffleSplit\n",
    "\n",
    "split = StratifiedShuffleSplit(n_splits=1, test_size=0.2)\n",
    "for train_indices,test_indices in split.split(iris_data,iris_data[[\"Species\",\"Petal.Width\",\"Sepal.Width\"]]):\n",
    "    train_df = iris_data.loc[train_indices]\n",
    "    test_df = iris_data.loc[test_indices]"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "X_train = train_df.drop(columns=\"Species\").to_numpy()\n",
    "y_train = train_df[[\"Species\"]].to_numpy()\n",
    "X_test = test_df.drop(columns=\"Species\").to_numpy()\n",
    "y_test = test_df[[\"Species\"]].to_numpy()"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}